import React from 'react';
import { message } from 'antd';
import { history } from 'umi'
import { useRequest } from '@/hooks';
import { checkLogin } from '../service/interview';

const useCheckLogin = (interviewNoticeId, from, manual = false) => {
    const [isLogin, setIsLogin] = React.useState(false);
    const push = path => {
        try {
            history.push({
                pathname: path,
                query: {
                    id: interviewNoticeId,
                    from
                }
            });
        } catch (error) {
            window.location.href = `${window.location.origin}${path}?id=${interviewNoticeId}`;
        }
    };

    const { run: handleCheckLogin, loading: checkLoading } = useRequest(() => checkLogin(interviewNoticeId), {
        manual,
        onSuccess: res => {
            const { code, msg, data: resData } = res;
            if (code === 10000) {
                if (resData?.wasLoginIn) {
                    setIsLogin(true);
                } else {
                    setIsLogin(false);
                    push('/recruitment/login');
                }
            } else if (msg) {
                message.error(msg);
            }
        }
    });

    return {
        handleCheckLogin,
        checkLoading,
        isLogin
    };
};

export default useCheckLogin;
